import CacheUtil from '@/common/CacheUtil.js'

function checkUpdate() {
	let urlstr = "https://zhiyuapp.oss-cn-fuzhou.aliyuncs.com/uniapp_android/worldConfig.json?t=" + new Date().getTime();
	uni.request({
		url: urlstr,
		success: (res) => {
			CacheUtil.setTicketOpen(res.data.vipManage)
			if (res.data.ticketTime) {
				getApp().globalData.ticketTotalTime = res.data.ticketTime
			}
			
			// #ifdef APP-PLUS
			plus.runtime.getProperty(plus.runtime.appid, (info) => {
				if (res.data.open == "1" && res.data.vss != info.version) {
					install(res.data.wgtUrl);
				}
			});
			// #endif
		}
	})
	
}


//wgt下载安装
function install(wgtUrl) {
	// #ifdef APP-PLUS
	plus.downloader.createDownload(wgtUrl, {
		filename: "_doc/update/"
	}, (d, status) => {
		if (status == 200) {
			plus.runtime.install(d.filename, {
				force: true
			}, function() {
				plus.nativeUI.closeWaiting();
				plus.runtime.restart();
			}, function(e) {
				plus.nativeUI.closeWaiting();
			});
		} else {
			plus.nativeUI.closeWaiting();
		}
	}).start();
	// #endif
}


module.exports = {
	checkUpdate
}